library(reshape2)
library(ggplot2)
library(ggthemes)

# Data ----
data <- data.frame(
  # Name = c("Apple", "Google", "Facebook", "Amozon", "Tencent"),
  Company = c("Apple", "Google", "Facebook", "Amozon", "Tencent"),
  Sale2013 = c(5000, 3500, 2300, 2100, 3100),
  Sale2014 = c(5050, 3800, 2900, 2500, 3300),
  Sale2015 = c(5050, 3800, 2900, 2500, 3300),
  Sale2016 = c(5050, 3800, 2900, 2500, 3300))

mydata <- melt(data, id.vars = "Company", 
               variable.name = "Year", value.name = "Sale")

ggplot(mydata, aes(Company, Sale, fill = Year)) +
  geom_bar(stat = "identity", position = "dodge")

# Themes ----
# Wall Street
ggplot(mydata, aes(Company, Sale, fill = Year)) +
  geom_bar(stat = "identity", position = "dodge") +
  theme_wsj() +
  scale_fill_wsj("rgby", "") +
  guides(fill = guide_legend(title = NULL)) +
  ggtitle("The Financial Performance of Five Giant") +
  theme(axis.title = element_blank())

# Econimist
ggplot(mydata, aes(Company, Sale, fill = Year)) +
  geom_bar(stat = "identity", position = "dodge") +
  theme_economist(base_size = 14) +
  scale_fill_economist() +
  guides(fill = guide_legend(title = NULL)) +
  ggtitle("The Financial Performance of Five Giant") +
  theme(axis.title = element_blank())


# Facet (horizontal)
ggplot(mydata, aes(Company, Sale, fill = Year)) +
  geom_bar(stat = "identity", position = "dodge") +
  theme_wsj() +
  scale_fill_wsj("rgby", "") +
  guides(fill = guide_legend(title = NULL)) +
  ggtitle("The Financial Performance of Five Giant") +
  theme(axis.title = element_blank(), legend.position = 'none') +
  facet_grid(. ~ Year)

ggplot(mydata, aes(Company, Sale, fill = Year)) +
  geom_bar(stat = "identity", position = "dodge") +
  facet_grid(. ~ Year) +
  theme_economist(base_size = 14) +
  scale_fill_economist() +
  guides(fill = guide_legend(title = NULL)) +
  ggtitle("The Financial Performance of Five Giant") +
  theme(
    axis.title = element_blank(),
    legend.position = 'none',
    axis.text.x = element_text(size = 7.5))

# Facet (Vertical)
ggplot(mydata, aes(Company, Sale, fill = Year)) +
  geom_bar(stat = "identity", position = "dodge") +
  theme_wsj() +
  scale_fill_wsj("rgby", "") +
  guides(fill = guide_legend(title = NULL)) +
  facet_grid(. ~ Year) +
  coord_flip() +
  ggtitle("The Financial Performance of Five Giant") +
  theme(
    axis.title = element_blank(),
    legend.position = 'none',
    axis.text.y = element_text(size = 10),
    axis.text.x = element_text(size = 7.5),
    axis.ticks.x = element_blank(),
    axis.ticks.y = element_line(),
    axis.ticks.length = unit(0.5, 'cm'),
    axis.line.y = element_line(),
    axis.line.x = element_blank(),
    panel.grid.major.x = element_line(linetype = "dashed", colour = "grey60"),
    panel.grid.major.y = element_blank())


ggplot(mydata, aes(Company, Sale, fill = Year)) +
  geom_bar(stat = "identity", position = "dodge") +
  theme_economist(base_size = 14) +
  facet_grid(. ~ Year) +
  coord_flip() +
  scale_fill_economist() +
  theme(axis.ticks.length = unit(0.5, 'cm')) +
  guides(fill = guide_legend(title = NULL)) +
  ggtitle("The Financial Performance of Five Giant") +
  theme(
    axis.title = element_blank(),
    legend.position = 'none',
    axis.text.y = element_text(size = 10),
    axis.text.x = element_text(size = 7.5),
    axis.line.x = element_blank(),
    axis.line.y = element_line(),
    axis.ticks.x = element_blank(),
    axis.ticks.y = element_line(),
    axis.ticks.length = unit(0.5, 'cm'),
    panel.grid.major.x = element_line(
      linetype = "solid",
      colour = "white",
      size = 1.1),
    panel.grid.major.y = element_blank())

# Facet (Vertical)

# Left
p1 <- ggplot(mydata, aes(Company, Sale, fill = Year)) +
  geom_bar(stat = "identity", position = "dodge") +
  theme_wsj() +
  scale_fill_wsj("rgby", "") +
  theme(axis.ticks.length = unit(0.3, 'cm')) +
  guides(fill = guide_legend(title = NULL)) +
  ggtitle("The Financial Performance of Five Giant") +
  theme(axis.title = element_blank(), legend.position = 'none') +
  facet_grid(Year ~ .)

# Right
p2 <- ggplot(mydata, aes(Company, Sale, fill = Year)) +
  geom_bar(stat = "identity", position = "dodge") +
  theme_economist(base_size = 14) +
  facet_grid(Year ~ .) +
  scale_fill_economist() +
  theme(axis.ticks.length = unit(0.3, 'cm')) +
  guides(fill = guide_legend(title = NULL)) +
  ggtitle("The Financial Performance of Five Giant") +
  theme(
    axis.title = element_blank(),
    legend.position = 'none',
    axis.text.y = element_text(size = 7.5),
    axis.text.x = element_text(size = 7.5, face = "bold"),
    strip.text = element_text(size = 10))

# Show all figures in a panel by multiplot()
require(grid)
library("Rmisc")
multiplot(p1, p2, cols = 2)

# Vertical facet ----
ggplot(mydata, aes(Company, Sale, fill = Year)) +
  geom_bar(stat = "identity", position = "dodge") +
  theme_wsj() +
  facet_grid(Year ~ .) +
  coord_flip() +
  scale_fill_wsj("rgby", "") +
  theme(axis.ticks.length = unit(0.5, 'cm')) +
  guides(fill = guide_legend(title = NULL)) +
  ggtitle("The Financial Performance of Five Giant") +
  theme(
    axis.title = element_blank(),
    legend.position = 'none',
    panel.grid.major.x = element_line(linetype = "dashed", colour = "grey60"),
    panel.grid.major.y = element_blank(),
    axis.ticks.x = element_blank(),
    axis.ticks.y = element_line(),
    axis.ticks.length = unit(0.3, 'cm'),
    axis.line.x = element_blank(),
    axis.line.y = element_line(),
    axis.text.x = element_text(size = 10))

ggplot(mydata, aes(Company, Sale, fill = Year)) +
  geom_bar(stat = "identity", position = "dodge") +
  facet_grid(Year ~ .) +
  coord_flip() +
  theme_economist(base_size = 14) +
  scale_fill_economist() +
  guides(fill = guide_legend(title = NULL)) +
  ggtitle("The Financial Performance of Five Giant") +
  theme(
    axis.title = element_blank(),
    legend.position = 'none',
    panel.grid.major.x = element_line(
      linetype = 1,
      colour = "white",
      size = 1.1),
    panel.grid.major.y = element_blank(),
    axis.ticks.x = element_blank(),
    axis.ticks.y = element_line(),
    axis.ticks.length = unit(0.2, 'cm'),
    axis.line.x = element_blank(),
    axis.line.y = element_line(),
    axis.text.x = element_text(size = 10),
    strip.text = element_text(size = 10),
    axis.text.y = element_text(size = 10)) 


# Barplot label----
ggplot(mydata, aes(Company, Sale, fill = Year, label = Sale)) +
  geom_bar(stat = "identity", position = "dodge") +
  theme_wsj() +
  scale_fill_wsj("rgby", "") +
  guides(fill = guide_legend(title = NULL)) +
  ggtitle("The Financial Performance of Five Giant") +
  geom_text(
    aes(y = Sale + 0.05),
    position = position_dodge(0.9),
    fontface = "bold",
    vjust = 1,
    colour = "white",
    size = 2) +
  theme(
    axis.title = element_blank(),
    axis.ticks.length = unit(0.3, 'cm'),
    axis.text.x = element_text(size = 10),
    axis.text.y = element_text(size = 10),)

ggplot(mydata, aes(Company, Sale, fill = Year, label = Sale)) +
  geom_bar(stat = "identity", position = "dodge") +
  theme_wsj() +
  facet_grid(. ~ Year) +
  scale_fill_wsj("rgby", "") +
  guides(fill = guide_legend(title = NULL)) +
  ggtitle("The Financial Performance of Five Giant") +
  geom_text(
    aes(y = Sale + 0.05),
    position = position_dodge(0.9),
    fontface = "bold",
    vjust = 1,
    colour = "white",
    size = 2) +
  theme(
    axis.title = element_blank(),
    legend.position = 'none',
    axis.ticks.length = unit(0.3, 'cm'),
    axis.line.x = element_line(),
    axis.text.x = element_text(size = 5),
    axis.text.y = element_text(size = 7.5))

# Horizontaol facet label
ggplot(mydata, aes(Company, Sale, fill = Year, label = Sale)) +
  geom_bar(stat = "identity", position = "dodge") +
  coord_flip() + facet_grid(. ~ Year) +
  theme_wsj() + scale_fill_wsj("rgby", "") +
  guides(fill = guide_legend(title = NULL)) +
  ggtitle("The Financial Performance of Five Giant") +
  geom_text(
    aes(y = Sale + 0.05),
    position = position_dodge(0.9),
    hjust = 1.2,
    vjust = 0.5,
    colour = "white",
    size = 4
  ) +
  theme(
    axis.title = element_blank(),
    legend.position = 'none',
    panel.grid.major.x = element_line(linetype = "dashed", colour = "grey60"),
    panel.grid.major.y = element_blank(),
    axis.ticks.x = element_blank(),
    axis.ticks.y = element_line(),
    axis.ticks.length = unit(0.3, 'cm'),
    axis.line.x = element_blank(),
    axis.line.y = element_line(),
    axis.text.x = element_text(size = 8),
    axis.text.y = element_text(size = 10)
  )

# Vertical facet label
ggplot(mydata, aes(Company, Sale, fill = Year, label = Sale)) +
  geom_bar(stat = "identity", position = "dodge") +
  theme_wsj() + scale_fill_wsj("rgby", "") +
  facet_grid(Year ~ .) + coord_flip() +
  guides(fill = guide_legend(title = NULL)) +
  ggtitle("The Financial Performance of Five Giant") +
  geom_text(
    aes(y = Sale + 0.05),
    position = position_dodge(0.9),
    hjust = 1.1,
    colour = "white",
    size = 3.5) +
  theme(
    axis.title = element_blank(),
    legend.position = 'none',
    panel.grid.major.x = element_line(linetype = "dashed", colour = "grey60"),
    panel.grid.major.y = element_blank(),
    axis.ticks.x = element_blank(),
    axis.ticks.y = element_line(),
    axis.ticks.length = unit(0.25, 'cm'),
    axis.line.x = element_blank(),
    axis.line.y = element_line(),
    axis.text.y = element_text(size = 10),
    axis.text.x = element_text(size = 10))

# ggsci package ----
library(ggsci)

p1 <- ggplot(subset(diamonds, carat >= 2.2), aes(table, price, color = cut)) +
    geom_point(alpha = 0.7) +
    geom_smooth(method = "loess", alpha = 0.05, size = 1, span = 1) +
    theme_bw()

p2 <- ggplot(subset(diamonds, carat > 2.2 & depth > 55 & depth < 70)) +
    geom_histogram(aes(depth, fill = cut), color = "black", binwidth = 1, position = "dodge") +
    theme_bw()

p1
p2

# Lancet
p1 + scale_color_lancet()
p2 + scale_fill_lancet()

# JAMA
p1 + scale_color_jama()
p2 + scale_fill_jama()

# NEJM
p1 + scale_color_nejm()
p2 + scale_fill_nejm()

# Nature
p1 + scale_color_npg()
p2 + scale_fill_npg()

# JCO
p1 + scale_color_jco()
p2 + scale_fill_jco()
